package com.quanyan.common.utils;

import com.quanyan.common.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BizTemplate<T> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected abstract void checkParams() throws BizException;

    protected abstract T process() throws BizException;

    protected void afterProcess() {

    }

    protected void onSuccess() {

    }

    protected void onFail() {

    }

    public T execute() throws BizException {
        try {
            checkParams();
            T result = process();
            onSuccess();
            return result;
        } catch (BizException e) {
            onFail();
            throw e;
        } catch (RuntimeException e) {
            logger.warn("error while execute", e);
            onFail();
            throw new BizException(e);
        } finally {
            afterProcess();
        }
    }

    protected void checkPrivilege(long userId) {

    }

}
